initEditor();

axios.get('/my/category/list').then(res => {
    let { status, data } = res.data;
    if (status === 0) {
        let str = '';
        data.forEach(item => {
            str += `<option value="${item.id}">${item.name}</option>`;
        });
        $('select[name=cate_id]').append(str);
        let form = layui.form;
        initEditor();

        // 页面处理，获取分类
        axios.get('/my/category/list').then(res => {
            let { status, data } = res.data;
            if (status === 0) {
                let str = '';
                data.forEach(item => {
                    str += `<option value="${item.id}">${item.name}</option>`;
                });
                $('select[name=cate_id]').append(str);
                // 更新渲染
                let form = layui.form;
                form.render('select');
            }
        });

        var $image = $('#image')
        var options = {
            aspectRatio: 400 / 280,
            preview: '.img-preview'
        }
        $image.cropper(options);

        $('button:contains("选择封面")').click(function () {
            $('#file').trigger('click');
        });

        $('#file').change(function () {
            if (this.files.length > 0) {
                var fileObj = this.files[0];
                var url = URL.createObjectURL(fileObj);
                $image.cropper('replace', url);
            }
        });


        // -------------------------- 表单提交，收集数据，ajax提交，完成添加 ---------------------
        $('form').on('submit', function (e) {
            e.preventDefault();
            let fd = new FormData(this);
            fd.set('content', tinyMCE.activeEditor.getContent());
            let canvas = $image.cropper('getCroppedCanvas', { width: 400, height: 280 });
            canvas.toBlob(function (blob) {
                fd.append('cover_img', blob);

                axios.post('/my/article/add', fd).then(res => {
                    let { status, message } = res.data;
                    if (status === 0) {
                        layer.msg(message)
                    }
                });
            });
        })
        form.render('select');
    }
});

let $image = $('#image')
let options = {
    aspectRatio: 400 / 280,
    preview: '.img-preview'
}
$image.cropper(options);

$('button:contains("选择封面")').click(function () {
    $('#file').trigger('click');
});

$('#file').change(function () {
    if (this.file.length > 0) {
        let fileObj = this.flie[0];
        let url = URL.createObjectURL(fileObj);
        $image.cropper('replace', url);
    }
});


$('form').on('submit', function (e) {
    e.preventDefault();
    let fd = new FormData(this);
    fd.set('content', tinyMCE.activeEditor.getContent());
    let canvas = $image.cropper('getCroppedCanvas', { width: 400, height: 280 });
    canvas.toBlob(function (blob) {
        fd.append('cover_img', blob);


        axios.post('/my/article/add', fd).then(res => {
            let { status, message } = res.data;
            if (status === 0) {
                layer.msg(message)
            }
        });
    });
})